package com.gzhryc.shared_device.cdcz.code.services;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.time.DateFormatUtils;

import com.gzhryc.common.StringTools;
import com.gzhryc.common.json.JsonTools;
import com.gzhryc.common.logging.Logger;
import com.gzhryc.jdbc.Conditions;
import com.gzhryc.jdbc.MultiDBTools;
import com.gzhryc.shared_device.base.models.OperateInfo;
import com.gzhryc.shared_device.base.models.OrderLogContent;
import com.gzhryc.shared_device.cdcz.code.dao.CDCZOrderChargeLogDao;
import com.gzhryc.shared_device.cdcz.code.dao.db.CDCZOrder;
import com.gzhryc.shared_device.cdcz.code.dao.db.CDCZOrderChargeLog;

public class CDCZOrderChargeLogService {

    static Logger log = Logger.getLogger(CDCZOrderChargeLogService.class);

    public static CDCZOrderChargeLogService self(Date date) {
        return MultiDBTools.getService(CDCZOrderChargeLogService.class, date);
    }

    public static CDCZOrderChargeLogService self(String jdbcKey,Date date) {
        return MultiDBTools.getService(jdbcKey, CDCZOrderChargeLogService.class, date);
    }

    CDCZOrderChargeLogDao dao;

    public CDCZOrderChargeLogService(String jdbcKey,Date date) {
        dao = new CDCZOrderChargeLogDao(jdbcKey,date);
    }

    public List<CDCZOrderChargeLog> findByTradeNo(String tradeNo){
        if(StringTools.isNotBlank(tradeNo)){
            Conditions conditions = new Conditions(CDCZOrderChargeLog.class);
            conditions.and().is("tradeNo",tradeNo);
            try {
                return dao.findByCondition(conditions);
            } catch (SQLException e) {
                log.error(e.getMessage(),e);
            }
        }
        return null;
    }

    public CDCZOrderChargeLog getByKeys(String tradeNo,String hourStr){
        if(StringTools.isNotBlank(tradeNo) && StringTools.isNotBlank(hourStr)) {
            Conditions conditions = new Conditions(CDCZOrderChargeLog.class);
            conditions.and().is("tradeNo",tradeNo);
            conditions.and().is("hourStr",hourStr);
            try {
                return dao.get(conditions);
            } catch (SQLException e) {
                log.error(e.getMessage(), e);
            }
        }
        return null;
    }

    public boolean add(CDCZOrder useOrder, OperateInfo operateInfo, Integer state, String errorMsg) {
        Date data = new Date();
        String hourStr = DateFormatUtils.format(data,"yyyyMMddHH");
        OrderLogContent logContent = null;
        CDCZOrderChargeLog orderLog = getByKeys(useOrder.getTradeNo(),hourStr);
        boolean toAdd = false;
        if (orderLog == null) {
            orderLog = new CDCZOrderChargeLog();
            orderLog.setTradeNo(useOrder.getTradeNo());
            orderLog.setHourStr(hourStr);
            orderLog.setCreateDate(useOrder.getCreateDate());
            logContent = new OrderLogContent();
            toAdd = true;
        } else {
            logContent = JsonTools.fromJson(orderLog.getContent(), OrderLogContent.class);
        }

        OrderLogContent.LogItem logItem = new OrderLogContent.LogItem();
        logItem.setCreateDate(new Date());
        logItem.setContent(operateInfo.getOperateNote());
        logItem.setProgramType(operateInfo.getProgramType());
        logItem.setProgramTypeName(operateInfo.getProgramTypeName());
        logItem.setOperatorType(operateInfo.getOperatorType());
        logItem.setOperatorTypeName(operateInfo.getOperatorTypeName());
        logItem.setOperator(operateInfo.getOperator());
        logItem.setState(state);
        logItem.setErrorMsg(errorMsg);
        logContent.getItems().add(logItem);

        orderLog.setContent(JsonTools.toJson(logContent));

        if (toAdd) {
            try {
                return dao.insert(orderLog) > 0;
            } catch (SQLException e) {
                log.error(e.getMessage(), e);
            }
        } else {
            Conditions conditions = new Conditions(CDCZOrderChargeLog.class);
            conditions.and().is("tradeNo",useOrder.getTradeNo());
            conditions.and().is("hourStr",hourStr);
            try {
                return dao.updateNotNull(orderLog,conditions) > 0;
            } catch (SQLException e) {
                log.error(e.getMessage(), e);
            }
        }
        return false;
    }

}
